package com.dykj.fireFighting.web.firemanger;

import java.util.*;

import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.PartitionsDevice;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IPartitionsDeviceService;
import com.dykj.fireFighting.api.socialUnit.firemanger.dto.KeyPartsDto;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.CoordinateRelation;
import com.dykj.fireFighting.api.socialUnit.firemanger.entity.KeyParts;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.ICoordinateRelationService;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.IKeyPartsDeviceService;
import com.dykj.fireFighting.api.socialUnit.firemanger.service.IKeyPartsService;
import com.dykj.fireFighting.api.socialUnit.firemanger.vo.KeyPartsDeviceVo;
import com.dykj.fireFighting.api.socialUnit.xfyl.service.IEmergencyPlanService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


import javax.validation.Valid;



/**
 * 消防管理-重点部位接口
 *
 * @Author aixiaolan
 * @Date  2021-06-25 14:04:07
 */
@Slf4j
@Api(value = "消防管理-重点部位接口", tags = {"消防管理-重点部位接口"})
@RestController
@RequestMapping("firemanger/keyParts")
public class KeyPartsController {

    @DubboReference
    private IKeyPartsService keyPartsService;

    @DubboReference
    private IKeyPartsDeviceService keyPartsDeviceService;

    @DubboReference
    private ICoordinateRelationService coordinateRelationService;

    @DubboReference
    private IEmergencyPlanService emergencyPlanService;

    @DubboReference
    private IPartitionsDeviceService partitionsDeviceService;


    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }
    /**
     * 消防管理-重点部位列表
     *
     * @return
     */
    @AutoLog(value = "消防管理-重点部位-分页列表查询")
    @ApiOperation(value = "消防管理-重点部位分页列表", notes = "消防管理-重点部位分页列表")
    @GetMapping("/list")
//    @RequiresPermissions("sms/keyParts/list")
    public CommonResult<PageVo<KeyParts>> treeGrid(KeyPartsDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<KeyParts> page = keyPartsService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取消防管理-重点部位信息
     */
    @AutoLog(value = "消防管理-重点部位-获取消防管理-重点部位信息")
    @ApiOperation(value = "获取消防管理-重点部位信息", notes = "获取消防管理-重点部位信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
//    @RequiresPermissions("sms/keyParts/info")
    public CommonResult<KeyPartsDeviceVo> info(@PathVariable("id") String id){
        KeyParts keyParts = keyPartsService.getById(id);
        KeyPartsDeviceVo keyPartsDeviceVo=new KeyPartsDeviceVo();
        BeanUtils.copyProperties(keyParts,keyPartsDeviceVo);
        List<String> device=keyPartsDeviceService.getByKeyPartsId(id,0);
        List<String> deviceTradtion=keyPartsDeviceService.getByKeyPartsId(id,1);
        keyPartsDeviceVo.setDevice(device);
        keyPartsDeviceVo.setDeviceTradition(deviceTradtion);
        if(StringUtils.isNotBlank(keyPartsDeviceVo.getPartitionId())){
            PartitionsDevice point = partitionsDeviceService.getPoint(keyPartsDeviceVo.getPartitionId(), id);
            log.info("分区关系表查询结果：{}",point.toString());
            if(point!=null){
                keyPartsDeviceVo.setLat( point.getX());
                keyPartsDeviceVo.setLng(point.getY());
            }
        }else{
            //关系表查找经纬度
            List<CoordinateRelation> coordinateRelations=coordinateRelationService.getByTableId(id,"0");
            log.info("关系表查询结果条数：{}",coordinateRelations.size());
            if(coordinateRelations.size()>0){
                keyPartsDeviceVo.setLat( coordinateRelations.get(0).getLat());
                keyPartsDeviceVo.setLng(coordinateRelations.get(0).getLng());
            }
        }

        log.info("查询结果：{}",keyPartsDeviceVo.toString());
        return CommonResult.success(keyPartsDeviceVo);
    }

    /**
     * 保存消防管理-重点部位信息
     */
    @AutoLog(value = "消防管理-重点部位-保存消防管理-重点部位信息")
    @ApiOperation(value = "保存消防管理-重点部位信息", notes = "保存消防管理-重点部位信息")
    @PostMapping("/save")
//    @RequiresPermissions("sms/keyParts/save")
    public CommonResult save(@Valid @RequestBody KeyPartsDeviceVo keyParts) {
        try {

            keyParts.setCreateBy(getName());
            keyParts.setUnitId(getEnterpriseId());
                keyPartsService.insertKeyParts(keyParts);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改消防管理-重点部位信息
     */
    @AutoLog(value = "消防管理-重点部位-修改消防管理-重点部位信息")
    @ApiOperation(value = "修改消防管理-重点部位信息", notes = "修改消防管理-重点部位信息")
    @PostMapping("/update")
//    @RequiresPermissions("sms/keyParts/update")
    public CommonResult update(@Valid @RequestBody KeyPartsDeviceVo keyParts) {
        try {
            keyParts.setUpdateBy(getName());
                keyPartsService.updateKeyParts(keyParts);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除消防管理-重点部位信息
     */
    @AutoLog(value = "消防管理-重点部位-删除消防管理-重点部位信息")
    @ApiOperation(value = "删除消防管理-重点部位信息", notes = "删除消防管理-重点部位信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
//    @RequiresPermissions("sms/keyParts/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                keyPartsService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
